package com.example.demo.controller;

import com.example.demo.model.User;
import com.example.demo.service.impl.AuthServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/auth")
public class AuthController {
    private final AuthServiceImpl authService;

    @Autowired
    public AuthController(AuthServiceImpl authService) {
        this.authService = authService;
    }

    @PostMapping("/register")
    public ResponseEntity<String> register(@RequestBody User user) {
        authService.register(user);
        return ResponseEntity.status(HttpStatus.CREATED).body("User registered successfully");
    }

    @PostMapping("/login")
    public ResponseEntity<String> login(@RequestParam String name, @RequestParam String password) {
        String token = authService.login(name, password);
        // 返回包含 token 的响应
        return ResponseEntity.ok(token);
    }

    @PostMapping("/{userId}/add-dish/{dishId}")
    public ResponseEntity<String> addDishToUser(@PathVariable Long userId, @PathVariable Long dishId) {
        authService.addDishToUser(userId, dishId);
        return ResponseEntity.ok("Dish added to user successfully");
    }

    @PostMapping("/{userId}/add-vieweddish/{dishId}")
    public ResponseEntity<String> addViewDishToUser(@PathVariable Long userId, @PathVariable Long dishId) {
        authService.addViewDishToUser(userId, dishId);
        return ResponseEntity.ok("viewedDish added to user successfully");
    }

    @PutMapping("/{userId}/share-dish/{dishId}/with/{otherUserId}")
    public ResponseEntity<String> shareDishWithUser(@PathVariable Long userId, @PathVariable Long dishId, @PathVariable Long otherUserId) {
        authService.shareDishWithUser(userId, dishId, otherUserId);
        return ResponseEntity.ok("Dish shared with user successfully");
    }
}


